package org.mybatis.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author yexi
 */
public class MyBatisUtil {
    private MyBatisUtil() {
    }

    private static SqlSessionFactory factory;

    static {
        try {
            //第一步：把主配置文件转成输入流
            InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
            //第二步：从流中构建出SqlSessionFactory对象
            factory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //第三步：从工厂对象中获取sqlSession对象
    public static SqlSession openSession() {
        return factory.openSession();
    }


}
